package com.example.datedemo;

import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.left_year)
    ImageView leftYear;
    @BindView(R.id.record_left)
    ImageView recordLeft;
    @BindView(R.id.record_title)
    TextView recordTitle;
    @BindView(R.id.record_right)
    ImageView recordRight;
    @BindView(R.id.right_year)
    ImageView rightYear;
    @BindView(R.id.record_gridView)
    GridView recordGridView;
    private int year;
    private int month;
    private String title;
    private int[][] days = new int[6][7];
    private DateAdapter dateAdapter;//定义adapter

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        //初始化日期数据
        initData();
        //初始化组件
        initView();
    }

    private void initData() {
        year = DateUtils.getYear();
        month = DateUtils.getMonth();
    }

    private void initView() {
        /**
         * 以下是初始化GridView
         */
        days = DateUtils.getDayOfMonthFormat(2016, 8);
        dateAdapter = new DateAdapter(this, days, year, month);//传入当前月的年
        recordGridView.setAdapter(dateAdapter);
        recordGridView.setVerticalSpacing(60);
        recordGridView.setEnabled(false);
        setTile();
    }

    /**
     * 设置标题
     */
    private void setTile() {
        title = year + "年" + month + "月";
        recordTitle.setText(title);
    }

    @OnClick({R.id.record_left, R.id.left_year, R.id.right_year, R.id.record_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left_year:
                days = prevyear();
                dateAdapter = new DateAdapter(this, days, year, month);
                recordGridView.setAdapter(dateAdapter);
                dateAdapter.notifyDataSetChanged();
                setTile();
                break;
            case R.id.right_year:
                days = nextyear();
                dateAdapter = new DateAdapter(this, days, year, month);
                recordGridView.setAdapter(dateAdapter);
                dateAdapter.notifyDataSetChanged();
                setTile();
                break;
            case R.id.record_left:
                days = prevMonth();
                dateAdapter = new DateAdapter(this, days, year, month);
                recordGridView.setAdapter(dateAdapter);
                dateAdapter.notifyDataSetChanged();
                setTile();
                break;
            case R.id.record_right:
                days = nextMonth();
                dateAdapter = new DateAdapter(this, days, year, month);
                recordGridView.setAdapter(dateAdapter);
                dateAdapter.notifyDataSetChanged();
                setTile();
                break;
        }
    }

    /**
     * 下一个月
     *
     * @return
     */
    private int[][] nextMonth() {
        if (month == 12) {
            month = 1;
            year++;
        } else {
            month++;
        }
        days = DateUtils.getDayOfMonthFormat(year, month);
        return days;
    }

    /**
     * 上一个月
     *
     * @return
     */
    private int[][] prevMonth() {
        if (month == 1) {
            month = 12;
            year--;
        } else {
            month--;
        }
        days = DateUtils.getDayOfMonthFormat(year, month);
        return days;
    }

    /**
     * 下一年
     *
     * @return
     */
    private int[][] nextyear() {
        year++;
        days = DateUtils.getDayOfMonthFormat(year, month);
        return days;
    }

    /**
     * 上一年
     *
     * @return
     */
    private int[][] prevyear() {
        year--;
        days = DateUtils.getDayOfMonthFormat(year, month);
        return days;
    }

}

